import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';

/// 加载本地资源图片
Image loadImage(
  String? name, {
  double? width,
  double? height,
  BoxFit? fit,
  String format = "png",
}) {
  return Image.asset(
    _getImgPath(name, format: format),
    height: height,
    width: width,
    fit: fit,
  );
}

/// 加载本地资源图片
AssetImage loadAssetImage(String name, {AssetBundle? bundle, String? package}) {
  return AssetImage(_getImgPath(name), bundle: bundle, package: package);
}

/// 加载网络图片
Widget loadNetworkImage(
  String? imageUrl, {
  String placeholder = "start_page",
  double? width,
  double? height,
  BoxFit fit = BoxFit.cover,
  bool animation = false,
}) {
  if (imageUrl == null || imageUrl == "") {
    return loadImage(
      placeholder,
      width: width,
      height: height,
      fit: fit,
    );
  }
  imageUrl = _imageUrlJoin(imageUrl);
  return CachedNetworkImage(
    imageUrl: imageUrl,
    placeholder: (context, url) => animation
        ? const Center(child: CupertinoActivityIndicator())
        : SizedBox(height: height, width: width),
    // placeholder: (context, url) => loadImage(placeholder, height: height, width: width, fit: fit),
    errorWidget: (context, url, error) =>
        loadImage(placeholder, height: height, width: width, fit: fit),
    width: width,
    height: height,
    fit: fit,
  );
}

/// 加载网络图片作为背景
/// [child] 子元素
/// 也可以使用该类进行加载,如:
/// decoration: BoxDecoration(
//     borderRadius: BorderRadius.circular(Dimens.gapDp8),
//     image: DecorationImage(
//       image: CachedNetworkImageProvider(widget?.item?.pic),
//       fit: BoxFit.fill,
//     ),
//   ),
Widget loadNetworkAssetImage(
  String? imageUrl, {
  Key? key,
  String placeholder = "start_page",
  double? width,
  double? height,
  EdgeInsetsGeometry? margin,
  EdgeInsetsGeometry? padding,
  BorderRadius? borderRadius,
  BoxFit fit = BoxFit.cover,
  bool animation = false,
  Widget? child,
}) {
  if (imageUrl == null || imageUrl == "") {
    return loadImage(
      placeholder,
      width: width,
      height: height,
      fit: fit,
    );
  }
  imageUrl = _imageUrlJoin(imageUrl);
  return CachedNetworkImage(
    key: key,
    imageUrl: imageUrl,
    useOldImageOnUrlChange: true,
    imageBuilder: (context, imageProvider) => Container(
      margin: margin,
      padding: padding,
      height: height,
      width: width,
      decoration: BoxDecoration(
        borderRadius: borderRadius ?? BorderRadius.circular(0.0),
        color: const Color(0xFF5E5E5E),
        image: DecorationImage(
          image: imageProvider,
          fit: fit,
        ),
      ),
      child: child,
    ),
    placeholder: (context, url) => animation
        ? const Center(child: CupertinoActivityIndicator())
        : SizedBox(height: height, width: width),
    errorWidget: (context, url, error) => loadImage(
      placeholder,
      height: height,
      width: width,
      fit: fit,
    ),
  );
}

String _imageUrlJoin(String url) {
  String imageUrl = url;
  // if (!url.startsWith(RegExp(r"https?:"))) {
  //   imageUrl = "${Configs.IMAGE_BASE_URL}/$url";
  //   List<String> s = url.split(":/");
  //   imageUrl = s[0] + ':/' + s[1].replaceAll("//", "/");
  // }
  // if (!imageUrl.contains("?")) {
  //   var width = ScreenUtil.screenWidth.toInt();
  //   var height = ScreenUtil.screenHeight.toInt();
  //   imageUrl += "?imageView2/1/w/$width/h/$height/format/jpg/q/50|imageslim";
  // }
  return imageUrl;
}

String _getImgPath(String? name, {String format = 'png'}) {
  return 'assets/images/$name.$format';
}
